-- 简单 Accept/Decline 按钮点击器
-- 专门针对确认的两个按钮

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local LocalPlayer = Players.LocalPlayer

local function log(msg)
	print("[SimpleAcceptDecline] " .. msg)
	local ok = pcall(function()
		appendfile("simple_accept_decline_log.txt", "[SimpleAcceptDecline] " .. msg .. "\n")
	end)
	if not ok then
		pcall(function()
			writefile("simple_accept_decline_log.txt", "[SimpleAcceptDecline] " .. msg .. "\n")
		end)
	end
end

-- 确保按钮及其父级链可见/可交互
local function ensureInteractable(btn)
	local p = btn
	while p and p:IsA("GuiObject") do
		pcall(function()
			p.Visible = true
			p.Active = true
			if p.ZIndex and p.ZIndex < 50 then p.ZIndex = 50 end
		end)
		p = p.Parent
	end
end

-- 利用 getconnections 直接触发所有已连接的回调（绕过输入限制）
local function fireAllConnections(btn)
	local fired = false
	local function fireSignal(signal)
		if not signal then return end
		local ok = pcall(function()
			if typeof(getconnections) == "function" then
				for _, conn in ipairs(getconnections(signal)) do
					pcall(function()
						if conn.Function then
							conn.Function()
						elseif conn.Fire then
							conn:Fire()
						end
						fired = true
					end)
				end
			end
		end)
		return ok
	end

	-- 优先触发 Activated 与 MouseButton1Click 等常见信号
	fireSignal(btn.Activated)
	fireSignal(btn.MouseButton1Click)
	fireSignal(btn.MouseButton1Down)
	fireSignal(btn.MouseButton1Up)
	fireSignal(btn.MouseEnter)
	fireSignal(btn.MouseLeave)

	return fired
end

-- 获取按钮
local function getButtons()
	local pg = LocalPlayer:FindFirstChild("PlayerGui")
	if not pg then return nil, nil end
	
	local giftNotification = pg:FindFirstChild("Gift_Notification")
	if not giftNotification then return nil, nil end
	
	local frame1 = giftNotification:FindFirstChild("Frame")
	if not frame1 then return nil, nil end
	
	local giftNotification2 = frame1:FindFirstChild("Gift_Notification")
	if not giftNotification2 then return nil, nil end
	
	local holder = giftNotification2:FindFirstChild("Holder")
	if not holder then return nil, nil end
	
	local frame2 = holder:FindFirstChild("Frame")
	if not frame2 then return nil, nil end
	
	local accept = frame2:FindFirstChild("Accept")
	local decline = frame2:FindFirstChild("Decline")
	
	return accept, decline
end

-- 简单点击方法
local function simpleClick(btn, buttonName)
	if not btn then 
		log("❌ " .. buttonName .. " 按钮不存在")
		return false 
	end
	
	log("点击 " .. buttonName .. " 按钮...")

	-- 确保按钮及父级可见并可交互
	ensureInteractable(btn)
	wait(0.1)
	
	-- 方法1: 直接 Activated
	local success = pcall(function()
		if btn.Activated then
			btn.Activated:Fire()
			log("✅ " .. buttonName .. " Activated 成功")
			return true
		end
	end)
	if success then return true end
	
	-- 方法2: Fire(Activated)
	success = pcall(function()
		btn:Fire("Activated")
		log("✅ " .. buttonName .. " Fire(Activated) 成功")
		return true
	end)
	if success then return true end
	
	-- 方法3: VirtualInputManager 模拟点击
	success = pcall(function()
		local pos = btn.AbsolutePosition
		local size = btn.AbsoluteSize
		local centerX = pos.X + size.X / 2
		local centerY = pos.Y + size.Y / 2
		game:GetService("VirtualInputManager"):SendMouseButtonEvent(centerX, centerY, 0, true, game)
		wait(0.05)
		game:GetService("VirtualInputManager"):SendMouseButtonEvent(centerX, centerY, 0, false, game)
		log("✅ " .. buttonName .. " VirtualInputManager 点击成功")
		return true
	end)
	if success then return true end

	-- 方法4: UserInputService 模拟点击
	success = pcall(function()
		local pos = btn.AbsolutePosition
		local size = btn.AbsoluteSize
		local centerX = pos.X + size.X / 2
		local centerY = pos.Y + size.Y / 2
		
		UserInputService:SendMouseButtonEvent(centerX, centerY, 0, true, game)
		wait(0.05)
		UserInputService:SendMouseButtonEvent(centerX, centerY, 0, false, game)
		log("✅ " .. buttonName .. " UserInputService 点击成功")
		return true
	end)
	if success then return true end
	
	-- 方法5: 直接触发所有已连接的回调
	local fired = fireAllConnections(btn)
	if fired then
		log("✅ " .. buttonName .. " getconnections 触发成功")
		return true
	end

	-- 方法6: 多次点击
	success = pcall(function()
		for i = 1, 5 do
			if btn.Activated then
				btn.Activated:Fire()
			end
			wait(0.1)
		end
		log("✅ " .. buttonName .. " 多次点击成功")
		return true
	end)
	if success then return true end
	
	log("❌ " .. buttonName .. " 所有方法都失败")
	return false
end

-- 点击 Accept
local function clickAccept()
	local accept, decline = getButtons()
	if accept then
		return simpleClick(accept, "Accept")
	else
		log("❌ 找不到 Accept 按钮")
		return false
	end
end

-- 点击 Decline
local function clickDecline()
	local accept, decline = getButtons()
	if decline then
		return simpleClick(decline, "Decline")
	else
		log("❌ 找不到 Decline 按钮")
		return false
	end
end

-- 自动接受
local function autoAccept()
	log("自动接受...")
	return clickAccept()
end

-- 设置监听
local function setupListener()
	log("设置监听...")
	
	local playerGui = LocalPlayer:WaitForChild("PlayerGui")
	
	-- 监听新按钮
	playerGui.DescendantAdded:Connect(function(descendant)
		if descendant:IsA("ImageButton") or descendant:IsA("TextButton") then
			local parent = descendant.Parent
			while parent do
				if parent.Name == "Gift_Notification" then
					local name = descendant.Name:lower()
					if name:match("accept") then
						log("检测到 Accept 按钮，自动点击...")
						wait(0.1)
						simpleClick(descendant, "Accept")
					end
					break
				end
				parent = parent.Parent
			end
		end
	end)
	
	-- 定期检查
	spawn(function()
		while true do
			local accept, decline = getButtons()
			if accept and accept.Visible then
				log("检测到可见的 Accept 按钮，自动点击...")
				clickAccept()
			end
			wait(1)
		end
	end)
end

-- 快捷键
UserInputService.InputBegan:Connect(function(input, gp)
	if gp then return end
	
	if input.KeyCode == Enum.KeyCode.A then
		clickAccept()
	elseif input.KeyCode == Enum.KeyCode.D then
		clickDecline()
	elseif input.KeyCode == Enum.KeyCode.Q then
		autoAccept()
	end
end)

-- 启动
log("简单 Accept/Decline 点击器已启动")
log("按 A 键点击 Accept")
log("按 D 键点击 Decline")
log("按 Q 键自动接受")

setupListener()
